'use strict'

const getOf = (list, key, value, defaultVal)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		if (obj[key] == value) {
			return obj;
		}
	}
	return defaultVal;
}

const getIndexOf = (list, key, value)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		if (obj[key] == value) {
			return i;
		}
	}
	return -1;
}

const getListAttrToOneList = (list, listAttrName)=>{
	let newList = [];
	for (var i = 0; i < list.length; i++) {
        let childList = list[i][listAttrName];
        if (childList != undefined && childList != null) {
        	for (var j = 0; j < childList.length; j++) {
	        	newList.push(childList[j]);
	        }
        }
    }
    return newList;
}

const getAttrToList = (list, key) => {
	let newList = [];
	for (var i = 0; i < list.length; i++) {
        let obj = list[i];
        newList.push(obj[key]);
    }
    return newList;
}


const setToList = (list, key, value)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		obj[key] = value;
	}
}

const setAttrBy = (list, key, value, byKey, byVal)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		if (obj[byKey] == byVal) {
			obj[key] = value;
		}
	}
}

const addBoolAttrFrom = (list, key, value, newKey)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		if (obj[key] == value) {
			obj[newKey] = true;
		}else {
			obj[newKey] = false;
		}
	}	
}

const filterList = (list, key, value)=>{
	let newList = new Array();
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		let objValue = obj[key];
		if (objValue == value) {
			newList.push(obj);
		}
	}
	return newList;
}

const removeFromList = (list, key, value)=>{
	let newList = new Array();
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		let objValue = obj[key];
		if (objValue != value) {
			newList.push(obj);
		}
	}
	return newList;
}

const joinToStr = (list, key, separator)=>{
	let newList = new Array();
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		newList.push(obj[key]);
	}
	return newList.join(separator);
}

const copyAttrTo = (list, fromKey, newKey)=>{
	for (let i = 0; i < list.length; i++) {
		let obj = list[i];
		obj[newKey] = obj[fromKey];
	}
}

const appendToList = (toList, fromList)=>{
	for (let i = 0; i < fromList.length; i++) {
		let obj = fromList[i];
		toList.push(obj);
	}
}

const copyObj = (obj)=>{
	let newObj = {};
	for(var key in obj) {
		newObj[key] = obj[key];
	}
	return newObj;
}

const arrContainVal = (arr, val) => {
	var i = arr.length;
    while (i--) {
        if (arr[i] == val) {
            return true;
        }
    }
    return false;
}

const isHitAllValue = (arrA, arrB) => {
	let hitCnt = 0;
	for (let i = 0; i < arrA.length; i++) {
		let valA = arrA[i];

		for (let j = 0; j < arrB.length; j++) {
			let valB = arrB[j];
			if (valA == valB) {
				hitCnt++;
				break;
			}
		}
	}
	return hitCnt == arrA.length;
}

const copyAttrsToObj = (toObj, fromObj, columns) => {
	let columnStr = columns.replace(/\s/g, '');
	let columnArr = columnStr.split(',');
	for (var i = 0; i < columnArr.length; i++) {
		let key = columnArr[i];
		toObj[key] = fromObj[key];
	}
}


export default{
	getOf, getIndexOf, getListAttrToOneList, getAttrToList, 
	setToList, setAttrBy, filterList, removeFromList, addBoolAttrFrom, 
	joinToStr,
	copyAttrTo, appendToList, copyAttrsToObj,
	copyObj,
	arrContainVal, isHitAllValue
}